package com.stackandqueue;

import java.util.Deque;
import java.util.LinkedList;

public class Solution04 {
    public boolean isValid(String s) {
      Deque<Character> deque = new LinkedList<>();
        char[] chars = s.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if (chars[i] == '(' ||chars[i] == '[' || chars[i] == '{') {
                deque.push(chars[i]);
            } else if (!deque.isEmpty()) {
                if ((chars[i] == ')' && deque.peek() == '(')
                || (chars[i] == ']' && deque.peek() == '[')
                || (chars[i] == '}' && deque.peek() == '{')) {
                    deque.poll();
                } else {
                    return false;
                }
            } else  {
                return false;
            }
        }  //"([)]"
        return deque.isEmpty();
    }
}
